package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.PayPswView;
import com.alm.token.ui.view.RegisterView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.LogUtil;
import com.alm.token.utils.MyOnclickListener;
import com.alm.token.utils.PopupAdapter;
import com.alm.token.utils.StringUtils;
import com.alm.token.utils.Utils;
import com.alm.token.widget.CommonPopupWindow;
import com.alm.token.widget.Countdownbutton;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import baibeiwallet.event.RefreshMeEvent;
import butterknife.BindView;
import butterknife.OnFocusChange;

import static com.alm.token.ConStant.county;
import static com.alm.token.ConStant.countyID;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_CODE_VAULE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_INTO_CODE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_NAME_VAULE;
import static com.alm.token.ui.activity.FundArealistActivity.INTENT_RESULT_SUCC_CODE;
import static com.alm.token.ui.persenter.MyPreenter.add_pay_password;
import static com.alm.token.ui.persenter.MyPreenter.back_pay_password;
import static com.alm.token.ui.persenter.MyPreenter.update_pay_password;

public class PayPwdActivity extends BaseActivity implements PayPswView<RegisterData> {

    @BindView(R.id.rly_oldpswbody)
    RelativeLayout rlyOldpswbody;
    @BindView(R.id.rly_inputauthCodebody)
    RelativeLayout rlyInputauthCodebody;
    @BindView(R.id.edt_inputoldpsw)
    EditText edtInputoldpsw;
    @BindView(R.id.edt_inputpswone)
    EditText edtInputpswone;
    @BindView(R.id.edt_inputpswonce)
    EditText edtInputpswonce;
    @BindView(R.id.edt_inputauthCode)
    EditText edtInputauthCode;
    @BindView(R.id.edt_inputphone)
    EditText edtInputphone;
    @BindView(R.id.edt_inputcode)
    EditText edtInputcode;
    //    @BindView(R.id.tv_getcode)
//    TextView tvGetcode;
    @BindView(R.id.tv_getcode)
    Countdownbutton tvGetcode;
    @BindView(R.id.tv_setphonecode)
    TextView tvSetphonecode;
    @BindView(R.id.tv_clickphonecountry)
    TextView tvClickphonecountry;
    @BindView(R.id.tv_mail)
    TextView tv_mail;
    @BindView(R.id.tv_pswsubmit)
    TextView tvPswsubmit;
    @BindView(R.id.tv_pswsubmit_findpsw)
    TextView tvPswsubmitFindpsw;
    private String strPhone, strCode, strOldPs, strPswone, strPswonce, strAuthCode, strinviteCode;

    public static final int PAYPSW_SET = 1, PAYPSW_CHANGE = 2, PAYPSW_FINDGET = 3;
    private int paypswtype = 0;

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_pay_psw;
    }

    @Override
    protected void initVariables() {
        preenter.getHavePayPwd();
    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.paypasw_title_setpaypsw));
        tv_mail.setText(StringUtils.nullToStr(MyApp.getMail()));
        if (MyApp.getMail() != null) {
            String[] split = MyApp.getMail().split("@");
            tv_mail.setText(split[0].length() > 4 ? split[0].substring(0, 2) + "****" + split[0].substring(split[0].length() - 2, split[0].length()) + "@" + split[1] : MyApp.getMail());
        }
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_clickphonecountry:
//                showPopupMenu2(tvSetphonecode);
//                showDownPop(tvSetphonecode);
                startActivityForResult(FundArealistActivity.class, INTENT_RESULT_INTO_CODE);

                break;
            case R.id.tv_pswsubmit_findpsw:
                Intent intent = new Intent(this, PayPwdActivity.class);
                intent.putExtra("PayPwdActivityType", PayPwdActivity.PAYPSW_FINDGET);
                startActivity(intent);
                finish();
                break;
            case R.id.tv_pswsubmit:

                if (paypswtype == PAYPSW_CHANGE) {
                    if (edtInputoldpsw.getText().toString().isEmpty() || edtInputoldpsw.getText().toString().length() != 6) {
                        showToast(getString(R.string.paypasw_hint_inputoldpaypsw));
                        edtInputoldpsw.setFocusable(true);
                        edtInputoldpsw.setFocusableInTouchMode(true);
                        edtInputoldpsw.requestFocus();
                        //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                        //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(edtInputoldpsw, 0);
                        break;
                    }
                }
                if (edtInputpswone.getText().toString().isEmpty() || edtInputpswone.getText().toString().length() != 6) {
                    showToast(getString(R.string.paypasw_hint_inputnewpaypsw));
                    edtInputpswone.setFocusable(true);
                    edtInputpswone.setFocusableInTouchMode(true);
                    edtInputpswone.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputpswone, 0);
                    break;
                }
                if (edtInputpswonce.getText().toString().isEmpty() || edtInputpswonce.getText().toString().length() != 6) {
                    showToast(getString(R.string.paypasw_hint_inputnewpaypswonce));
                    edtInputpswonce.setFocusable(true);
                    edtInputpswonce.setFocusableInTouchMode(true);
                    edtInputpswonce.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputpswonce, 0);
                    break;
                }
//                if (edtInputphone.getText().toString().isEmpty()) {
//                    showToast(getString(R.string.profile_hint_warn6));
//                    edtInputphone.setFocusable(true);
//                    edtInputphone.setFocusableInTouchMode(true);
//                    edtInputphone.requestFocus();
//                    //显示软键盘
////                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
//                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.showSoftInput(edtInputphone, 0);
//                    break;
//                }
                if (edtInputcode.getText().toString().isEmpty()) {
                    showToast(getString(R.string.profile_hint_warn11));
                    edtInputcode.setFocusable(true);
                    edtInputcode.setFocusableInTouchMode(true);
                    edtInputcode.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputcode, 0);
                    break;
                }

//                if (paypswtype == PAYPSW_FINDGET) {
//                    if (edtInputauthCode.getText().toString().isEmpty()) {
//                        showToast(getString(R.string.findpasw_hint_inputauthCode));
//                        edtInputauthCode.setFocusable(true);
//                        edtInputauthCode.setFocusableInTouchMode(true);
//                        edtInputauthCode.requestFocus();
//                        //显示软键盘
////                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//                        //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
//                        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
//                        imm.showSoftInput(edtInputauthCode, 0);
//                        break;
//                    }
//                }

                strPhone = edtInputphone.getText().toString();
                strCode = edtInputcode.getText().toString();
                strOldPs = edtInputoldpsw.getText().toString();
                strPswone = edtInputpswone.getText().toString();
                strPswonce = edtInputpswonce.getText().toString();
                strAuthCode = edtInputauthCode.getText().toString();

                if (!strPswone.equals(strPswonce)) {
                    showToast(getString(R.string.paypasw_hint_inputnewpaypsw_worm));
                    break;
                }
//
//                if (strPswone.length() < 8 || strPswone.length() > 20) {
//                    showToast(getString(R.string.profile_hint_warn33));
//                    break;
//                }
//                if (!Utils.verifyUserNameFL(strPswone)) {//[A-Za-z1-9]{2}
//                    showToast(getString(R.string.profile_hint_warn44));
//                    break;
//                }
//                if (!Utils.verifyUserPsw(strPswone)) {
//                    showToast(getString(R.string.profile_hint_warn22));
//                    break;
//                }

//                if (!(strPswone.substring(0)
//                        + strPswone.substring(strPswone.length() - 1))
//                        .matches("[0-9A-Za-z]*")) {
//                    showToast(getString(R.string.profile_hint_warn4));
//                    break;
//                }

//                showToast("提交注册 " + "strUserName= " + strUserName + "  strPhone=" + strPhone + " strCode=" + strCode
//                        + " strPswone=" + strPswone + " strPswonece=" + strPswone + " strinviteCode=" + strinviteCode);

//                MyApp.Token="";
//                if(preenter==null){
//                    AppClient.mRetrofit=null;
//                    preenter=new MyPreenter(this);
//                }
                if (paypswtype == PAYPSW_SET) {
                    preenter.getAddPayPwdData(strPswone, strPswonce, strCode);
                } else if (paypswtype == PAYPSW_CHANGE) {
                    preenter.getEditPayPwdData(strOldPs, strPswone, strPswonce, strCode);
                } else if (paypswtype == PAYPSW_FINDGET) {
                    preenter.getPayPwdGetBackData(strPswone, strPswonce, strAuthCode, strCode);
                }
                break;

            case R.id.tv_getcode:
//                strPhone = edtInputphone.getText().toString();
//                if (strPhone.isEmpty()) {
//                    showToast(getString(R.string.profile_hint_warn6));
//                    edtInputphone.setFocusable(true);
//                    edtInputphone.setFocusableInTouchMode(true);
//                    edtInputphone.requestFocus();
//                    //显示软键盘
////                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
//                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.showSoftInput(edtInputphone, 0);
//                    break;
//                } else if (!Utils.isPhoneNumberValid("+" + Integer.parseInt(tvSetphonecode.getText().toString()) + strPhone,
//                        Integer.parseInt(tvSetphonecode.getText().toString()) + "")) {
//
//                    showToast(getString(R.string.profile_hint_warn7));
//                    edtInputphone.setFocusable(true);
//                    edtInputphone.setFocusableInTouchMode(true);
//                    edtInputphone.requestFocus();
//                    //显示软键盘
////                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
//                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.showSoftInput(edtInputphone, 0);
//                    break;
//                }
////                showToast("点击获取验证码");
////                MyApp.Token="";
////                if(preenter==null){
////                    AppClient.mRetrofit=null;
////                    preenter=new MyPreenter(this);
////                }
//                tvGetcode.start();
//                LogUtil.d("9999验证码" + "+" + Integer.parseInt(tvSetphonecode.getText().toString()) + strPhone);
//
//                String key=paypswtype==PAYPSW_SET?MyPreenter.add_pay_password:paypswtype==PAYPSW_CHANGE?MyPreenter.update_pay_password:paypswtype==PAYPSW_FINDGET?MyPreenter.back_pay_password:"";
                tvGetcode.start();
                preenter.sendMailCode(MyApp.getMail(), paypswtype == PAYPSW_SET ? add_pay_password :
                        paypswtype == PAYPSW_CHANGE ? update_pay_password : back_pay_password);
//                preenter.getSmsCodeData(strPhone);
                break;
            default:
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_RESULT_INTO_CODE && resultCode == INTENT_RESULT_SUCC_CODE && data != null) {
            String name = data.getStringExtra(INTENT_RESULT_NAME_VAULE);
            String code = data.getStringExtra(INTENT_RESULT_CODE_VAULE);
            LogUtil.e("name=" + name + "  code=" + code);

            tvSetphonecode.setText(code);
            tvClickphonecountry.setText(name);
        }
    }

    @OnFocusChange({R.id.edt_inputphone, R.id.edt_inputcode,
            R.id.edt_inputpswone, R.id.edt_inputpswonce})
    void onEdiFocusChange(View view, boolean hasFocus) {
        switch (view.getId()) {
            case R.id.edt_inputphone:

                break;

            case R.id.edt_inputcode:

                break;
            case R.id.edt_inputpswone:

                break;
            case R.id.edt_inputpswonce:

                if (hasFocus && edtInputpswone.getText().toString().isEmpty()) {
                    showToast(getString(R.string.profile_hint_warn9));
                    edtInputpswone.setFocusable(true);
                    edtInputpswone.setFocusableInTouchMode(true);
                    edtInputpswone.requestFocus();
                    //显示软键盘
//                     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    //如果上面的代码没有弹出软键盘 可以使用下面另一种方式
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(edtInputpswone, 0);
                    break;
                }

                break;
        }


    }

    @Override
    public void getData(RegisterData registerData) {
        showToast(registerData.getMsg());
        if (registerData.getStatus() == 0) {
            EventBus.getDefault().post(new RefreshMeEvent(1));
            finish();
        }

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this, PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType", PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog(getString(R.string.profile_hint_warn17));
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    @Override
    public void sendMailCode(RegisterData registerData) {
        showToast(registerData.getMsg());
    }

    @Override
    public void getCode(RegisterData registerData) {
        if (registerData.getMsg().equalsIgnoreCase(getString(R.string.profile_hint_warn15))) {
            showToast(getString(R.string.profile_hint_warn14));
        } else {
            showToast(registerData.getMsg());
        }
    }

    @Override
    public void getHavePayPwd(RegisterData isHavePayPwd) {
        //"data": 1, // 1 有 0 没有
        if (isHavePayPwd.getData().equalsIgnoreCase("1")) {
            if (getIntent().getIntExtra("PayPwdActivityType", 2) == PAYPSW_CHANGE) {
                rlyOldpswbody.setVisibility(View.VISIBLE);
                rlyInputauthCodebody.setVisibility(View.GONE);
                paypswtype = PAYPSW_CHANGE;
                tvPswsubmitFindpsw.setVisibility(View.VISIBLE);
                new ActionBarHelper(this).setTitle(getString(R.string.paypasw_title_changepaypsw));
            } else {
                rlyOldpswbody.setVisibility(View.GONE);
//                rlyInputauthCodebody.setVisibility(View.VISIBLE);
                rlyInputauthCodebody.setVisibility(View.GONE);
                paypswtype = PAYPSW_FINDGET;
                tvPswsubmitFindpsw.setVisibility(View.GONE);
                new ActionBarHelper(this).setTitle(getString(R.string.paypasw_title_findpaypsw)).setLeft(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(PayPwdActivity.class);
                        finish();
                    }
                });
            }
        } else {
            rlyOldpswbody.setVisibility(View.GONE);
            rlyInputauthCodebody.setVisibility(View.GONE);
            tvPswsubmitFindpsw.setVisibility(View.GONE);
            paypswtype = PAYPSW_SET;
        }
        tvPswsubmit.setOnClickListener(this);
        tvPswsubmitFindpsw.setOnClickListener(this);
        tvGetcode.setOnClickListener(this);
        tvClickphonecountry.setOnClickListener(this);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 && paypswtype == PAYPSW_FINDGET) {
            startActivity(PayPwdActivity.class);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
